<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Item;
use Auth;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index()
    {
        /*$environment = \App::environment();
        dd($environment);*/
        //phpinfo();exit;
        $user = array(
            'a' => array(100, 'a1'),
            'b' => array(101, 'a2'),
            'c' => array(102, 'a3'),
            'd' => array(103, 'a4'),
            'e' => array(104, 'a5'),
            'f' => 2,
        );
        /*$user = array(
        'a' => array(100, 'a1'),
        'a' => array(100, 'a1'),
        'c' => array(
        'd' => array(102, 'a3'),
        'e' => array(103, 'a4'),
        ),
        );*/

        /*     $result = array_reduce($user, function ($result, $value) {
        return array_merge($result, array_values($value));
        }, array());
        dd($result);
        $result = array_reduce($user, 'array_merge', array());
        dd($result);*/
        /*$user = ['1', 2, 23, [2, 3], [4, 56, [8, 9]]];

        $result = [];
        array_walk_recursive($user, function ($value) use (&$result) {
        array_push($result, $value);
        });

        dd($result);*/
        /*$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
        dd($array);*/
        // dd(\Redis::get('p1'));
        /*$parameters = array(
        'tcp://114.215.222.20:7031','tcp://114.215.222.20:7032','tcp://114.215.222.20:7033'
        );
        $options = [
        'cluster' => 'predis',
        'parameters' => ['password' => 'rxres123456']
        ];
        $client = new Predis\Client($parameters, $options);*/
        /* dd(\Redis::set('md1',11112));*/
        //$redis =Redis::connection('my-cluster');
        /*$redis =app('redis')->connection('default');
        dd($redis->get('p3'));*/
        // dd(Redis::get('p1'));
        //$people=\App\Helpers\Factory::createPeople('man');
        //$people=\App\Helpers\Factory::createWomen();
        /* $people=new \App\Helpers\Factoryman();
        $man=$people->createObj();
        $man->jiehun();
        $instance=\App\Helpers\Single::getInstance();
        $ss=getlist();
        var_dump($ss);
        dd($instance->getName());*/
        $user = Auth::guard('admin')->user();
        /* Notification::send($user, new Invoices($user));*/
        //Mail::to($user)->send(new OrderShipped($user));
        //
        /*$name = '小李'.'你好：';
        $imgPath = 'http://laravelacademy.org/wp-statics/images/carousel/LaravelAcademy.jpg';
        //$imgPath=storage_path('app/files/1.jpg');
        //$image = Storage::get('files/1.jpg');
        Mail::send('emails.name',['name'=>$name],function($message){
        $to = '1020472637@qq.com';
        $attachment = storage_path('app/files/123.docx');
        //在邮件中上传附件
        $message->attach($attachment,['as'=>"=?UTF-8?B?".base64_encode('网站说明')."?=.docx"]);
        });*/
        //return new OrderShipped($user);
        //$user->notify(new Invoices($user))
        //dd(Auth::guard('admin')->user());
        //dd(Auth::guard('admin')->id());
        /*$role = Role::create(['name' => 'agent','guard_name'=>'admin']);
        $user->assignRole('agent');
        dd($user->hasRole('agent'));*/
        $items = Item::orderBy('id', 'desc')->paginate();
        /* \DB::connection()->enableQueryLog();
        $items = Item::orderBy('id', 'desc')->take(2)->skip(2)->get()->toArray();
        $log = \DB::getQueryLog();
        dd($items);*/
        return view('admin.index', ['items' => $items]);
    }
    public function info()
    {
        /* \DB::connection()->enableQueryLog();
        $query=\DB::table('items')->orderBy('id', 'desc');
        $keyword="2'";
        if(!empty($keyword))
        {
        $query=$query->where('name', 'like', '%' . $keyword . '%');
        $count=Item::where('name', 'like', '%' . $keyword . '%')->count();
        }
        $items = $query->take(2)->skip(0)->get();
        $log = \DB::getQueryLog();
        dd($log);*/
        /*$str='<a href="test.html">测试口</a>';
        $transstr = htmlspecialchars($str,ENT_QUOTES) ; // 转换双引号和单引号
        echo $transstr . "<br />";
        echo htmlspecialchars_decode($transstr);
        dd(4545);*/
        dd('admin-info');
    }
    public function page()
    {
        return view('admin.page');
    }
    public function item(Request $request)
    {
        $pageNum = $request->pageNum;
        $pageSize = $request->pageSize;
        $keyword = $request->keyword;
        if (empty($pageNum)) {
            $pageNum = 1;
        }
        if (empty($pageSize)) {
            $pageSize = 2;
        }
        $startNum = ($pageNum - 1) * $pageSize;
        $count = Item::count();
        $query = Item::orderBy('id', 'desc');
        if (!empty($keyword)) {
            $query = $query->where('name', 'like', '%' . $keyword . '%');
            $count = Item::where('name', 'like', '%' . $keyword . '%')->count();
        }
        $items = $query->take($pageSize)->skip($startNum)->get();
        return response()->json(['totalCount' => $count, 'items' => $items]);
    }

    public function excel()
    {
        return view('admin.excel', ['items' => array()]);
    }
    public function update(Request $request)
    {
        dd($request->all());
    }
    public function web()
    {
        return view('admin.web');
    }
    public function doweb(Request $request)
    {
        return response()->json(['name' => $request->name]);
        $messages = [
            'name.required' => '请输入商品名称',
            'name.max' => '商品名称最长 :max 个字符',
            'price.required' => '请输入商品价格',
            'price.integer' => '商品价格必须是正整数',
            'stock.required' => '请输入商品库存',
            'stock.integer' => '商品库存必须是正整数',
        ];
        $this->validate($request, [
            'name' => 'required|max:255',
            'price' => 'required|integer',
            'stock' => 'required|integer',
        ], $messages);

        $item = new Item();
        $item->user_id = Auth::guard('admin')->user()->id;
        $item->category_id = $request->category_id ?? 1;
        $item->name = $request->name;
        $item->price = intval($request->price) * 100;
        $item->stock = $request->stock;
        $item->details = $request->details;
        if ($item->save()) {
            return redirect('/admin/web')->with(['msg' => '商品创建成功', 'code' => 200]);
        } else {
            return redirect('/admin/web')->with(['msg' => '商品创建失败', 'code' => 500]);
        }
    }
    public function checkEmail()
    {
        return response()->json(['valid' => true]);
    }
    public function tooltip()
    {
        return view('admin.tooltip');
    }
    public function doadd(Request $request)
    {
        return response()->json(['name' => $request->pname]);
    }
    public function web4()
    {
        return view('admin.web4');
    }
    public function property(Request $request)
    {
        $data = [];
        $id = $request->id;
        $pro = $request->property;
        $num = $request->num;
        $price = $request->price;
        if (empty($pro)) {
            return abort(404);
        }
        foreach ($pro as $key => $value) {
            $data[$key]['item_id'] = $id;
            $data[$key]['property_value_id'] = $pro[$key];
            $data[$key]['num'] = intval($num[$key]);
            $data[$key]['price'] = intval($price[$key]) * 100;
        }
        return response()->json(['data' => $data]);
    }
    public function react()
    {
        return view('admin.react');
    }

    public function jsonp(Request $request)
    {
        $item = Item::orderBy('id', 'desc')->paginate();
        return response()->json(['data' => $item]);
    }
    public function test()
    {
        return view('admin.test');
    }
    public function upload()
    {
        return view('admin.upload');
    }
}
